package com.simple.elpmobile.onlinecase.ui.component;

import java.util.ArrayList;

import com.simple.elpmobile.domain.Doctor;
import com.simple.elpmobile.onlinecase.R;
import com.simple.elpmobile.onlinecase.common.data.BaseGlobalVaribles;
import com.simple.elpmobile.onlinecase.engine.AppEngine;
import com.simple.elpmobile.onlinecase.engine.IManager;
import com.simple.elpmobile.onlinecase.engine.manager.NetworkManager;
import com.simple.elpmobile.onlinecase.engine.network.ResponseHandler.CommanHandler;
import com.simple.elpmobile.onlinecase.ui.base.CustomToast;
import com.simple.elpmobile.onlinecase.ui.base.LoadingDialog;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class FragmetTab1 extends Fragment {
	
	private ListView mListView;
	private LoadingDialog loadingDialog;
	
	public static FragmetTab1 newInstance() {
		FragmetTab1 newFragment = new FragmetTab1();
		return newFragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.tab1_layout, container, false);
		mListView=(ListView)view.findViewById(R.id.doctor_list);
		initData();
		return view;
	}
	
	public void initData(){
		showLoading("数据加载中...");
		((NetworkManager) (AppEngine.getInstance()
				.getManager(IManager.NETWOTK_ID))).getDoctorInfo( new CommanHandler() {
					@Override
					public void onLoginSuccess(Object obj) {
						dismissLoading();
						ArrayList<Doctor> doctors=(ArrayList<Doctor>)obj;
						BaseGlobalVaribles.doctors=doctors;
						DocListAdapter mAdapter=new DocListAdapter(getActivity(), doctors);
						mListView.setAdapter(mAdapter);
					}

					@Override
					public void onLoginFailed(String errorDescription) {
						dismissLoading();
						CustomToast.showToast(getActivity(),errorDescription, 1000);
					}
				});
	}
	
	private final String DIALOG_LOCKER = "dialogLocker";

	private void showLoading(String msg) {
		synchronized (DIALOG_LOCKER) {
			if (loadingDialog != null) {
				loadingDialog.dismissDialog();
				loadingDialog = null;
			}
			loadingDialog = new LoadingDialog(getActivity());
			loadingDialog.showDialog(msg);
		}
	}

	private void dismissLoading() {
		synchronized (DIALOG_LOCKER) {
			if (loadingDialog != null) {
				loadingDialog.dismissDialog();
				loadingDialog = null;
			}
		}
	}
	
	
	@Override
	public View getView() {
		return super.getView();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
}
